<?php
exit;
/**
 * @param unknown_type $str
 * 存在-并且 -后面不是字母
 * @return true 是后缀  false不是后缀
 */
$data=array();
function isFix($str){
	$str=trim($str);
	if (is_numeric(substr($str, 0,1)))
	return true;
	return false;
	
}

function readFix($i){
	global $lines,$data;
	$str = trim($lines[$i]);
	$pos=strpos($str, "、");
	$root=substr($str, $pos+3);
	if(strstr($lines[$i+1],"=")){
		$explan=trim($lines[++$i]);
	}else{
		$explan="";
	}
	$array=array('root'=>$root,'ex'=>$explan);
	for ($i++;!isFix($lines[$i])&&$i<count($lines);$i++){
		$str = trim($lines[$i]);
		$tt = preg_split("/\s+/", $str);
		$array['words'][]=array($tt[0],trim(substr($str, strlen($tt[0]))));
	}
	$data[]=$array;
	return $i;
}


$dir=__DIR__."/cg";
$contents= file_get_contents($dir);
$contents=trim($contents);
$lines=explode("\n", $contents);
$tNum = count($lines);
for ($i=0;$i<$tNum;){//echo $i."\n";
	
	if (isFix($lines[$i])){
		$i=readFix($i);	
	}elseif ($i<$tNum){
		die("错误发生").$i." --";
	}
	if($i>3800){
		$j=0;
		$j++;
	}
}
echo "读取完毕";
print_r(array_keys($data));
print_r(($data));
echo count($data);

//exit;
mysql_connect("localhost","root","");
mysql_select_db("diandi_danci");
mysql_query("set names utf8");
foreach ($data as $root=> $row){
	mysql_query("INSERT INTO `diandi_danci`.`root` (`id`, `root`, `alias`, `info`) VALUES (NULL, '".$row['root']."', '', '".$row['ex']."');");
	$rootId= mysql_insert_id();

		
			foreach ($row['words'] as $word){
				//$word[0]//1
				$re = mysql_query("select * from word where word='".$word[0]."'");
				if ($r=mysql_fetch_array($re)){
					$wordId=$r['id'];
				}else{
					mysql_query("INSERT INTO `diandi_danci`.`word` (`id`, `word`, `symbolA`, `symbolE`, `pronunciationA`, `pronunciationE`) VALUES (NULL, '".$word[0]."', '', '', '', '');");
					$wordId=mysql_insert_id();
				}
				mysql_query("INSERT INTO `diandi_danci`.`word_root` (`id`, `word_id`, `root_id`, `explanation`, `info`) VALUES (NULL, '".$wordId."', '".$rootId."', '', '".$word[1]."');");
			
		
	}
	
	
	
}